﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms.DataVisualization.Charting;



namespace SSVisionCom
{
    public class AutoZone
    {
        #region 控件大小随窗体大小等比例缩放

        public float TheWidth { set; get; }//定义当前窗体的宽度
        public float TheHeight { set; get; }//定义当前窗体的高度

        public void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;
                if (con.Controls.Count > 0) setTag(con);
            }
        }

        private void setControls(float newx, float newy, Control cons)
        {
            //遍历窗体中的控件，重新设置控件的值
            foreach (Control con in cons.Controls)
                //获取控件的Tag属性值，并分割后存储字符串数组
                if (con.Tag != null)
                {
                    var mytag = con.Tag.ToString().Split(';');
                    //根据窗体缩放的比例确定控件的值
                    con.Width = Convert.ToInt32(Convert.ToSingle(mytag[0]) * newx); //宽度
                    con.Height = Convert.ToInt32(Convert.ToSingle(mytag[1]) * newy); //高度
                    con.Left = Convert.ToInt32(Convert.ToSingle(mytag[2]) * newx); //左边距
                    con.Top = Convert.ToInt32(Convert.ToSingle(mytag[3]) * newy); //顶边距
                    var currentSize = Convert.ToSingle(mytag[4]) * newy; //字体大小                   
                    if (currentSize > 0) con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                    con.Focus();
                    if (con.Controls.Count > 0) setControls(newx, newy, con);
                }
        }


        /// <summary>
        /// 重置窗体布局
        /// </summary>
        public void ReWinformLayout(float Width, float Height, Control con)
        {
            var newx = Width / TheWidth;
            var newy = Height / TheHeight;
            setControls(newx, newy, con);

        }

        #endregion
    }
    public class ProductManager
    {
        public int ProductCount = 0;
        public int ProductOKCount = 0;
        public int ProductNGCount = 0;
        public string DataPath = "";
        public double ProductYield = 0;

        public ProductManager(string Data_path)
        {
            this.DataPath = Data_path;
            try
            {
                ProductCount = Convert.ToInt16(OpearationTxt.ReadIni(DateTime.Now.Date.ToString(), "ProductCount", DataPath));
                ProductOKCount = Convert.ToInt16(OpearationTxt.ReadIni(DateTime.Now.Date.ToString(), "ProductOKCount", DataPath));
                ProductNGCount = Convert.ToInt16(OpearationTxt.ReadIni(DateTime.Now.Date.ToString(), "ProductNGCount", DataPath));
            }
            catch
            {
                ProductCount = 0;
                ProductNGCount = 0;
                ProductOKCount = 0;

            }
        }
        public void NGAdd()
        {
            try
            {
                ProductCount = Convert.ToInt16(OpearationTxt.ReadIni(DateTime.Now.Date.ToString(), "ProductCount", DataPath));
                ProductOKCount = Convert.ToInt16(OpearationTxt.ReadIni(DateTime.Now.Date.ToString(), "ProductOKCount", DataPath));
                ProductNGCount = Convert.ToInt16(OpearationTxt.ReadIni(DateTime.Now.Date.ToString(), "ProductNGCount", DataPath));
            }
            catch
            {
                ProductCount = 0;
                ProductNGCount = 0;
                ProductOKCount = 0;

            }
            ProductCount++;
            ProductNGCount++;

            try
            {
                OpearationTxt.WriteIni(DateTime.Now.Date.ToString(), "ProductCount", ProductCount.ToString(), DataPath);
                OpearationTxt.WriteIni(DateTime.Now.Date.ToString(), "ProductOKCount", ProductOKCount.ToString(), DataPath);
                OpearationTxt.WriteIni(DateTime.Now.Date.ToString(), "ProductNGCount", ProductNGCount.ToString(), DataPath);
            }
            catch
            {
                Console.WriteLine("保存产能统计失败！");
            }

        }
        public void OKAdd()
        {
            try
            {
                ProductCount = Convert.ToInt16(OpearationTxt.ReadIni(DateTime.Now.Date.ToString(), "ProductCount", DataPath));
                ProductOKCount = Convert.ToInt16(OpearationTxt.ReadIni(DateTime.Now.Date.ToString(), "ProductOKCount", DataPath));
                ProductNGCount = Convert.ToInt16(OpearationTxt.ReadIni(DateTime.Now.Date.ToString(), "ProductNGCount", DataPath));
            }
            catch
            {
                ProductCount = 0;
                ProductNGCount = 0;
                ProductOKCount = 0;

            }
            ProductCount++;
            ProductOKCount++;

            try
            {
                OpearationTxt.WriteIni(DateTime.Now.Date.ToString(), "ProductCount", ProductCount.ToString(), DataPath);
                OpearationTxt.WriteIni(DateTime.Now.Date.ToString(), "ProductOKCount", ProductOKCount.ToString(), DataPath);
                OpearationTxt.WriteIni(DateTime.Now.Date.ToString(), "ProductNGCount", ProductNGCount.ToString(), DataPath);
            }
            catch
            {
                Console.WriteLine("保存产能统计失败！");
            }
        }

        public void TotalClear()
        {

            try
            {
                ProductCount = Convert.ToInt16(OpearationTxt.ReadIni(DateTime.Now.Date.ToString(), "ProductCount", DataPath));
                ProductOKCount = Convert.ToInt16(OpearationTxt.ReadIni(DateTime.Now.Date.ToString(), "ProductOKCount", DataPath));
                ProductNGCount = Convert.ToInt16(OpearationTxt.ReadIni(DateTime.Now.Date.ToString(), "ProductNGCount", DataPath));
            }
            catch
            {
                ProductCount = 0;
                ProductNGCount = 0;
                ProductOKCount = 0;

            }
            ProductCount = 0;
            ProductNGCount = 0;
            ProductOKCount = 0;

            try
            {
                OpearationTxt.WriteIni(DateTime.Now.Date.ToString(), "ProductCount", ProductCount.ToString(), DataPath);
                OpearationTxt.WriteIni(DateTime.Now.Date.ToString(), "ProductOKCount", ProductOKCount.ToString(), DataPath);
                OpearationTxt.WriteIni(DateTime.Now.Date.ToString(), "ProductNGCount", ProductNGCount.ToString(), DataPath);
            }
            catch
            {
                Console.WriteLine("保存产能统计失败！");
            }

        }

        public string Yield()
        {
            string result = "";
            if (ProductCount == 0)
            {
                result = "0";
            }
            if (ProductNGCount == 0 && ProductCount != 0)
            {
                result = "100%";
            }
            else if (ProductNGCount != 0)
            {
                result = (((double)ProductOKCount / (double)ProductCount) * 100).ToString("0.00") + "%";
            }
            else
            {
                result = "0";
            }
            return result;
        }
    }

    public static class OpearationTxt
    {
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string value, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        [DllImport("kernel32.dll")]
        static extern uint GetTickCount();


        /// <summary>  
        /// 读ini文件  
        /// </summary>  
        /// <param name="section">节</param>  
        /// <param name="key">键</param>  
        /// <param name="strPath">配置文件路径</param>  
        /// <returns></returns>  
        public static string ReadIni(string section, string key, string strPath)
        {
            try
            {
                StringBuilder temp = new StringBuilder(1024);
                int i = GetPrivateProfileString(section, key, "", temp, 1024, strPath);
                string result = temp.ToString();
                if (result.Contains("\\r\\n"))
                {
                    result = result.Replace(("\\r\\n"), ("\r\n"));
                }
                return result;
            }
            catch
            {
                return "";
            }

        }

        /// <summary>  
        /// 写入ini文件  
        /// </summary>  
        /// <param name="section">节</param>  
        /// <param name="key">键</param>  
        /// <param name="value">值</param>  
        /// <param name="strPath">文件路径</param>  
        /// <returns></returns>  
        public static void WriteIni(string section, string key, string value, string strPath)
        {
            if (value.Contains("\r\n"))
            {
                value = value.Replace(("\r\n"), ("\\r\\n"));
            }
            WritePrivateProfileString(section, key, value, strPath);
        }
        /// <summary>  
        /// 删除节  
        /// </summary>  
        /// <param name="section">节</param>  
        /// <param name="strPath"></param>  
        /// <returns></returns>  
        public static long DeleteSection(string section, string strPath)
        {
            return WritePrivateProfileString(section, null, null, strPath);
        }

        /// <summary>  
        /// 删除键  
        /// </summary>  
        /// <param name="section">节</param>  
        /// <param name="key">键</param>  
        /// <param name="strPath">文件路径</param>  
        /// <returns></returns>  
        public static long DeleteKey(string section, string key, string strPath)
        {
            return WritePrivateProfileString(section, key, null, strPath);
        }
    }
}
